<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
<html>
<head>
<script src="../../resources/js-test.js"></script>
</head>
<body>
<p id="description"></p>
<div id="console"></div>
<script>
description("Tests webkitGetUserMedia.");

var stream;
var errorArg;

function error() {
    testFailed('Error callback called.');
    finishJSTest();
}

function gotStreamInError(s) {
    testFailed('Stream generated.');
    finishJSTest();
}

function gotStream5(s) {
    stream = s;
    testPassed('Stream generated.');
    shouldBe('stream.getAudioTracks().length', '1');
    shouldBe('stream.getVideoTracks().length', '1');

    finishJSTest();
}

function error1(e) {
    errorArg = e;
    testPassed('Error callback called.');
    shouldBeEqualToString('errorArg.name', 'OverconstrainedError');
    shouldBeEqualToString('errorArg.constraint', 'valid_but_unsupported_1');

    shouldNotThrow("navigator.getUserMedia({audio:{mandatory:{'valid_and_supported_1':1}, optional:[{'valid_but_unsupported_1':0}]}, video:true}, gotStream5, error);");
}

function gotStream4(s) {
    stream = s;
    testPassed('Stream generated.');
    shouldBe('stream.getAudioTracks().length', '1');
    shouldBe('stream.getVideoTracks().length', '1');

    shouldNotThrow("navigator.getUserMedia({audio:{mandatory:{'valid_but_unsupported_1':0}, optional:[]}, video:true}, gotStreamInError, error1);");
}

function gotStream3(s) {
    stream = s;
    testPassed('Stream generated.');
    shouldBe('stream.getAudioTracks().length', '1');
    shouldBe('stream.getVideoTracks().length', '1');

    shouldNotThrow("navigator.getUserMedia({audio:{mandatory:{}, optional:[]}, video:true}, gotStream4, error);");
}

function gotStream2(s) {
    stream = s;
    testPassed('Stream generated.');
    shouldBe('stream.getAudioTracks().length', '0');
    shouldBe('stream.getVideoTracks().length', '1');

    shouldNotThrow("navigator.getUserMedia({audio:true, video:true}, gotStream3, error);");
}

function gotStream1(s) {
    stream = s;
    testPassed('Stream generated.');
    shouldBe('stream.getAudioTracks().length', '1');
    shouldBe('stream.getVideoTracks().length', '0');

    shouldNotThrow("navigator.getUserMedia({video:true}, gotStream2, error);")
}

function gotStreamNoMore(s) {
    stream = s;
    testPassed('Stream generated.');
    shouldBe('stream.getAudioTracks().length', '1');
    shouldBe('stream.getVideoTracks().length', '0');
}

var GetUserMediaPrefixed = 1400;
var GetUserMediaLegacy = 1401;
var GetUserMediaPromise = 1402;

if (window.internals) {
  shouldBeFalse('internals.isUseCounted(document, GetUserMediaPrefixed)');
  shouldBeFalse('internals.isUseCounted(document, GetUserMediaLegacy)');
}

// Prefixed version
shouldThrow("navigator.webkitGetUserMedia({audio:false, video:false}, error, error);")
shouldNotThrow("navigator.webkitGetUserMedia({audio:true}, gotStreamNoMore, error);");
// Unprefixed version
shouldThrow("navigator.getUserMedia({audio:false, video:false}, error, error);")
shouldNotThrow("navigator.getUserMedia({audio:true}, gotStream1, error);");

if (window.internals) {
  shouldBeTrue('internals.isUseCounted(document, GetUserMediaPrefixed)');
  shouldBeTrue('internals.isUseCounted(document, GetUserMediaLegacy)');
  shouldBeFalse('internals.isUseCounted(document, GetUserMediaPromise)');
}

window.jsTestIsAsync = true;
window.successfullyParsed = true;
</script>
</body>
</html>
